<?php

class pdb { //extends

	private $_pdo;

	public function __construct($dsn, $user, $passwd ) {
		$this->_pdo	= new PDO($dsn, $user, $passwd);
		//PDO::ERRMODE_SILENT
		//PDO::ERRMODE_WARNING
		//PDO::ERRMODE_EXCEPTION
		$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	}



	final function  & __call($method, $args){
		if( ! method_exists($this->_pdo , $method)  ) {
			throw new Exception( 'dynamic method [[PDO::'.$method.']] is not exists');
			return null;
		}	
		$result	=& call_user_func_array( array($this->_pdo ,$method) , $args );
		return $result;
	}


	public public function & one($qstr){
		$re	=& $this->_pdo->query($qstr);
		$re->setFetchMode(PDO::FETCH_ASSOC);
		return $re->fetch();
	}


	public function insert($table, array $bind) {
		
		return $this->_pdo->lastInsertId();
	}

}

/*

class base extends ::core::wrapper {

	const	Quote	= '`';
	
	public function __construct($dsn, $user, $passwd ) {
		$pdb = new ::PDO($dsn, $user, $passwd);
		parent::__construct($pdb);
	}

	public function __destruct(){
		$this->body = null;
	}

	protected function afterCall( $result ){
		$this->isErr($this->body);
		if( ::is_object($result)) $this->isErr($result);
		return false;
	}

	protected function isErr( &$err ){
		$code	= $err->errorCode();
		if( $code !=='00000' && $code !=='' ){
			$klass	= ::get_class($err);
			$e	= $err->errorInfo();
			if( $klass == 'PDOStatement' ){
				array_push($e, 'queryString=[' . $err->queryString . ']' );
			}
			throw new ::native::Exception( '['.$klass ."] throw Exception\n" . ' SQLSTATE['.$e[0].']['.$e[1].'] '.$e[2]  );
		}
		return false;
	}

	// data format method
	
	protected function _quoteIdentifier($value){
		return $value ;
	}

	// operate method
	public function &one()
	{
		$args	= ::func_get_args();
		$re	= $this->query(::call_user_func_array( '::sprintf' , $args));
		$re->setFetchMode(::PDO::FETCH_ASSOC);
		return $re->fetch();
	}

	public function insert($table, array $bind) {
		$cols = array();
		$vals = array();

		foreach ($bind as $col => $val) {
			$cols[] = $this->_quoteIdentifier($col, true);
			$vals[] = '?';
		}

		$sql = "INSERT INTO "
			. $this->_quoteIdentifier($table, true)
			. ' (' . ::implode(', ', $cols) . ') '
			. 'VALUES (' . ::implode(', ', $vals) . ')';

		$sth = $this->prepare($sql);
		$sth->execute(::array_values($bind));
		$this->isErr($sth);
		return $this->lastInsertId();
	}

	public function update($table, array $bind, $where = '')
	{
		$set = array();
		foreach ($bind as $col => $val) {
			$set[] = $this->_quoteIdentifier($col, true) . ' =?';
		}

		//$where = $this->_whereExpr($where);

		$sql = "UPDATE "
			 . $this->_quoteIdentifier($table, true)
			 . ' SET ' . ::implode(', ', $set)
			 . (($where) ? " WHERE $where" : '');
		$sth = $this->prepare($sql);
		$sth->execute(::array_values($bind));
		$this->isErr($sth);
	}

	public function delete($table, $where = ''){
		$where = $this->_whereExpr($where);
		$sql = "DELETE FROM "
			 . $this->quoteIdentifier($table, true)
			 . (($where) ? " WHERE $where" : '');
		$stmt = $this->query($sql);
		$result = $stmt->rowCount();
		return $result;
	}

	protected function _whereExpr($where)
	{
		if (empty($where)) {
			return $where;
		}
		if (!is_array($where)) {
			$where = array($where);
		}
		foreach ($where as &$term) {
			if ($term instanceof Zend_Db_Expr) {
				$term = $term->__toString();
			}
			$term = '(' . $term . ')';
		}
		$where = implode(' AND ', $where);
		return $where;
	}
}
*/

?>